package pro.cherish.knowledgenotifywebsocket.web;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-09-18 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：RequestParamController
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-09-18 cherish
 */
@RestController
@RequestMapping("/test/request")
public class RequestParamController {
    @PostMapping("t1")
    public void test01() throws IOException {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        System.out.println(servletRequestAttributes);
        HttpServletRequest request = servletRequestAttributes.getRequest();
        BufferedReader br = request.getReader();
        String str;
        StringBuilder wholeStr = new StringBuilder();
        HashMap<String, Object> map = new HashMap<>();
        while ((str = br.readLine()) != null) {
            wholeStr.append(str);
        }
        String paramsArray[] = StringUtils.split(wholeStr.toString(), "&");
        for (String p : paramsArray) {
            String deCodeStr = URLDecoder.decode(p, "utf-8");
            int splitIndex = deCodeStr.indexOf("=");
            String key = deCodeStr.substring(0, splitIndex);
            String value = deCodeStr.substring(splitIndex + 1);
            map.put(key, value);
        }
    }
    @PostMapping("t2")
    public void test02(HttpServletRequest request) throws IOException {
        Object name = request.getAttribute("name");
        Map<String, String[]> parameterMap = request.getParameterMap();
        String[] ages = parameterMap.get("age");



    }
}
